package uebung5;

import java.io.PrintStream;

abstract class StudentBase {
	
	enum Course {INF,WINF,PEET};
	
	private final int m_MNr;
	private String m_Name;
	private String m_Surname;
	private final Date m_Birth;
	private int m_Exams;
			
	public StudentBase(int iMNr,String name,String surname,Date birth) {
		m_MNr = iMNr;
		m_Name = name;
		m_Surname = surname;
		m_Birth = birth;
		m_Exams = 0;
	}
	
	public int mnr() {
		return m_MNr;
	}
	
	public int age() {
		return new Date().year() - m_Birth.year();
	}
	
	public int nrOfPassedExams() {
		int iRes = 0;
		for(int iExams = m_Exams;iExams != 0;iExams >>= 1) {
			if ((iExams & 1) != 0) {
				++iRes;
			}
		}
		return iRes;
	}
	
	public boolean hasPassedExams(int iExam) {
		return (m_Exams & iExam) == iExam;
	}
	
	public void print(PrintStream p) {
		p.println("Vorname, Name: " + surname() + ", " + name());
		p.println("Matrikelnummer: " + mnr());
		p.println("Geboren: " + date());
		p.println("Studiengang: " + course());
		p.println("Fachbereich: " + department());
		p.print("bestandene Pr�fungen: ");
		printPassedExams(p);
		p.println();
	}
	
	public void printPassedExams(PrintStream p) {
		boolean bFirst = true;
		for(int i=1,iExams = m_Exams;iExams != 0;iExams >>= 1,++i) {
			if ((iExams & 1) != 0) {
				if (bFirst)
					bFirst = false;
				else
					p.print(", ");
				p.print(i);
			}
		}
	}
	
	public Date date() {
		return m_Birth;
	}
	
	public String name() {
		return m_Name;
	}
	
	public String surname() {
		return m_Surname;
	}
	
	public abstract Course course();
	
	public int department() {
		switch(course()) {
			case INF:case WINF: return 2;
			case PEET: return 1;
		}
		return 0;
	}
}

class InfStud extends StudentBase {
	InfStud(int iMNr,String name,String surname,Date birth) {
		super(iMNr,name,surname,birth);
	}

	public Course course() {
		return Course.INF;
	}
}

class WinfStud extends StudentBase {
	WinfStud(int iMNr,String name,String surname,Date birth) {
		super(iMNr,name,surname,birth);
	}

	public Course course() {
		return Course.WINF;
	}
}

class PeetStud extends StudentBase {
	PeetStud(int iMNr,String name,String surname,Date birth) {
		super(iMNr,name,surname,birth);
	}

	public Course course() {
		return Course.PEET;
	}
}